﻿
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WpfAGVSystem.Common
{
    public class ComboxBehavior : Behavior<HandyControl.Controls.ComboBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
        }
        /// <summary>
        /// 读内容与更新内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var combox = sender as HandyControl.Controls.ComboBox;
            var value = ComboxEx.GetClearValueProperty(combox);
            if (combox.SelectedIndex == -1) 
            {
                combox.SelectedValue = value;
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching(); AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
        }
    }


    public class ComboxEx
    {

        public static object GetClearValueProperty(DependencyObject obj)
        {
            return obj.GetValue(ClearValueProperty);
        }

        public static void SetClearValueProperty(DependencyObject obj, object value)
        {
            obj.SetValue(ClearValueProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClearValueProperty =
            DependencyProperty.RegisterAttached("ClearValue", typeof(object), typeof(ComboxEx), new PropertyMetadata(null));


        public static string GetSelets(DependencyObject obj)
        {
            return (string)obj.GetValue(SeletsProperty);
        }
       
        public static void SetSelets(DependencyObject obj, string value)
        {
            obj.SetValue(SeletsProperty, value);
        }
        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SeletsProperty =
            DependencyProperty.RegisterAttached("Selets", typeof(string), typeof(ComboxEx), new PropertyMetadata("", OnPropertyChanged));


        static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
                string pw = (string)e.NewValue;
                if (string.IsNullOrEmpty(pw)) return;
                var combox = sender as HandyControl.Controls.CheckComboBox;
                var list = pw.Split(',', StringSplitOptions.RemoveEmptyEntries);
                bool flag = false;
                    if (list.Length != combox.SelectedItems.Count)
                    {
                        flag = true;
                    }
                    for (int i = 0; i < combox.SelectedItems.Count; i++)
                    {
                        var a = combox.SelectedItems[i] as Val_Des;
                        if (a == null)
                        {
                            flag = true;
                            break;
                        }
                        if (list[i].ToString() != a.Val.ToString())
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        combox.SelectedItems.Clear();
                        var tt = combox.ItemsSource as ObservableCollection<Val_Des>;
                        foreach (var item in list)
                        {
                            combox.SelectedItems.Add(tt.FirstOrDefault(x => x.Val.ToString() == item));
                        }
                    }
        }
     
    }
    public class ComboxExBehavior : Behavior<HandyControl.Controls.CheckComboBox> 
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
        }
        /// <summary>
        /// 读内容与更新内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var combox = sender as HandyControl.Controls.CheckComboBox;
            var list = ComboxEx.GetSelets(combox);
            string result=string.Empty;
            foreach (var item in combox.SelectedItems)
            {
                Val_Des val_Des=item as Val_Des;
                result += val_Des?.Val+",";
            }
            if (result != list)
            {
                ComboxEx.SetSelets(combox, result);     
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching(); AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
        }
    }
    public class PassWordExtensions
    {
        public static string GetPassWord(DependencyObject obj)
        {
            return (string)obj.GetValue(PassWordProperty);
        }

        public static void SetPassWord(DependencyObject obj, string value)
        {
            obj.SetValue(PassWordProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PassWordProperty =
            DependencyProperty.RegisterAttached("PassWord", typeof(string), typeof(PassWordExtensions), new PropertyMetadata(string.Empty, OnPassWordPropertyChanged));

        static void OnPassWordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var passWord = sender as PasswordBox;
            string pw = (string)e.NewValue;
            if (passWord != null && passWord.Password != pw)
            {
                passWord.Password = pw;
            }
        }
    }


    public class PasswordBehavior : Behavior<PasswordBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PasswordChanged += AssociatedObject_PasswordChanged;
        }
        /// <summary>
        /// 读内容与更新内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_PasswordChanged(object sender, RoutedEventArgs e)
        {
            var passWord = sender as PasswordBox;
            string pw = PassWordExtensions.GetPassWord(passWord);
            if (passWord != null && passWord.Password != pw)
            {
                PassWordExtensions.SetPassWord(passWord, passWord.Password);
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching(); AssociatedObject.PasswordChanged -= AssociatedObject_PasswordChanged;
        }
    }

}
